/*
 * Copyright (c) 2022-2023 cetcclout Co., Ltd All rights reserved.
 * Sensor Adapter is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package com.cetccloud.cimp.common.util;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jsonschema.core.report.ProcessingMessage;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchemaFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author
 * @apiNote schema校验工具
 * @date 2022/12/23
 */
public class SchemaValidatorUtil {
    private static  final String METHOD_INOUT_PROS_KEY="properties";
//    private static  final String METHOD_INOUT_REQUIRED_KEY="required";
    private static final ObjectMapper mapper = new ObjectMapper();
//    private static String mSchema = ResourceUtil.readUtf8Str("cimp_v1.0.1.json");

    /**
     * 判断jsonData中schema是否正确
     */
    public static ValidState validSchema(final String jsonData, JsonNode schemaNode){
        ValidState validState = new ValidState();
        try {

            JsonNode scmProNodes = schemaNode.findPath(METHOD_INOUT_PROS_KEY);
//            JsonNode scmRequired = schemaNode.findPath(METHOD_INOUT_REQUIRED_KEY);
            //properties
            if (scmProNodes == null || scmProNodes.size() == 0) {
                validState.setSuccess(false);
                validState.setMsg("not need valid ");
                return validState;
            }
            JsonNode contentNode = mapper.readTree(jsonData);
            ProcessingReport report = JsonSchemaFactory.byDefault().getValidator()
                    .validateUnchecked(schemaNode,
                            contentNode);
            if (report.isSuccess()) {
                validState.setSuccess(true);
                validState.setMsg("valid success");
            } else {
                validState.setSuccess(false);
                List<JsonNode> errorsJsonArray = new ArrayList<>();
                Iterator<ProcessingMessage> iterator = report.iterator();
                while (iterator.hasNext()) {
                    errorsJsonArray.add(iterator.next().asJson());
                }
                validState.setMsg(errorsJsonArray);
                return validState;
            }
        }catch (Exception ex){
            ex.printStackTrace();
            validState.setSuccess(false);
            validState.setMsg(ex.getMessage());
        }
        return validState;
    }

    /**
     * 校验返回信息对象
     */
    public static class ValidState {
        private boolean success;
        private Object msg;

        public void setSuccess(boolean success) {
            this.success = success;
        }

        public boolean getSuccess() {
            return success;
        }

        public void setMsg(Object msg) {
            this.msg = msg;
        }

        public Object getMsg() {
            return msg;
        }
    }
}
